<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <table border="">
      <thead>
        <tr>
          <td>姓名</td>
          <td>年龄</td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>张三</td>
          <td>20</td>
        </tr>
      </tbody>
    </table>

    <script>
      var arr = [
        {
          name: "张三",
          age: 20,
        },
        {
          name: "李四",
          age: 30,
        },
        {
          name: "王五",
          age: 20,
        },
        {
          name: "王小二",
          age: 20,
        },
        {
          name: "王小二",
          age: 20,
        },
        {
          name: "王小二",
          age: 20,
        },
        {
          name: "王小二",
          age: 20,
        },
        {
          name: "王小二",
          age: 20,
        },
      ];
      var htmlstr = "";
      var tbody = document.querySelector("tbody");
      for (var i = 0; i < arr.length; i++) {
        if (i % 2 == 0) {
          //偶数行
          htmlstr +=
            "<tr style=background:cyan><td>" +
            arr[i].name +
            "</td><td>" +
            arr[i].age +
            "</td></tr>";
        } else {
          //奇数行
          htmlstr +=
            "<tr style=background:yellow><td>" +
            arr[i].name +
            "</td><td>" +
            arr[i].age +
            "</td></tr>";
        }
      }
      tbody.innerHTML = htmlstr;
    </script>
  </body>
</html>
